package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavors;
import com.itheima.jsonresult.JsonResult;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorsService;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linying
 * @since 2022-10-13
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorsService dishFlavorsService;

    /**
     * 新增菜品
     * @param dish
     * @return
     */
    @Override
    public JsonResult add(Dish dish) {
        // 根据username查询是否存在
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", dish.getName());

        // 查询结果为空，不存在
        if ((getOne(wrapper) == null)) {
            // 先添加dish
            save(dish);
            // 循环遍历添加flavors
            for (DishFlavors f : dish.getFlavors()) {
                DishFlavors flavors = new DishFlavors();
                flavors.setName(f.getName());
                flavors.setDishId(dish.getId());
                flavors.setValue(f.getValue());
                flavors.setShowOption(f.getShowOption());
                dishFlavorsService.save(flavors);
            }
            return new JsonResult(1, null, "新增菜品成功",null);
        } else {
            return new JsonResult(0, null, dish.getName() + "已存在",null);
        }
    }

    /**
     *修改菜品
     * @param dish
     * @return
     */
    @Override
    public JsonResult put(Dish dish) {
        // 添加修改的条件name
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", dish.getName());
        // 根据name修改dish表
        update(dish, wrapper);
        // 获取flavors的数组值
        for (DishFlavors f : dish.getFlavors()) {
            DishFlavors flavors = new DishFlavors();
            flavors.setName(f.getName());
            flavors.setValue(f.getValue());
            flavors.setShowOption(f.getShowOption());
            // 修改flavors的值
            QueryWrapper fwrapper = new QueryWrapper();
            wrapper.eq("dish_id", dish.getId());
            dishFlavorsService.remove(wrapper);
            dishFlavorsService.save(flavors);
        }
        return JsonResult.success("修改菜品成功");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @Override
    public JsonResult delete(String ids) {
        String[] split = ids.split(",");

        //数组转集合
        List<String> list = Arrays.asList(split);

        removeByIds(list);
        return JsonResult.success("删除成功");
    }

    /**
     * 菜品停售/起售
     * @param status
     * @param ids
     * @return
     */
    @Override
    public JsonResult status(String status, String ids) {
        //切割ids字符串获取数组
        String[] split = ids.split(",");
        //数组转集合
        List<String> id = Arrays.asList(split);

        //遍历id集合
        for (String s : id) {
            //根据id获取dish对象
            Dish dish = getById(s);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("id", dish.getId());

            //判断status的值
            if (dish.getStatus()==0) {
                dish.setStatus(1);
                //根据id修改status的值
                update(dish, wrapper);
            } else {
                dish.setStatus(0);
                update(dish, wrapper);
            }
        }
        return new JsonResult(1, null, "操作成功",null);
    }

    /**
     * 菜品下拉列表
     * @return
     */
    @Override
    public JsonResult dishList(String categoryId) {
        //根据categoryId,查询全部
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id", categoryId);
        List<Dish> list = list(wrapper);

        //遍历集合设置flavors的值
        for (Dish dish : list) {
            QueryWrapper fwrapper = new QueryWrapper();
            wrapper.eq("dish_id", dish.getId());
            List<DishFlavors> flist = dishFlavorsService.list(fwrapper);

            //将查询到的DishFlavors存入响应的dish中
            dish.setFlavors(flist);
        }

        return JsonResult.success(list);
    }

    /**
     * 菜品信息分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult getPage(String name, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        // 姓名可选非空条件判断
        if (!StrUtil.isBlank(name)) {
            wrapper.eq("name", name);
        }
        // 调用分页查询方法
        Page jsonpage = (Page) page(new Page<>(page, pageSize), wrapper);

        return JsonResult.success(jsonpage);
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @Override
    public JsonResult getDishById(String id) {
        // 1、查询菜品表
        Dish dish = getById(id);
        // 2、查询口味表
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dish_id", id);
        List<DishFlavors> list = dishFlavorsService.list(wrapper);
        // 3、重新装到菜品类里
        dish.setFlavors(list);

        return new JsonResult(1,null,dish,null);
    }

    /**
     * 菜品列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Cacheable(value = "dish", key = "'dishList'+#categoryId")
    @Override
    public List<Dish> dishListclient(String categoryId, String status) {
        //根据分类id和状态查询
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("status", status);
        List<Dish> list = list(wrapper);

        //遍历集合存入口味
        for (Dish dish : list) {
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("dish_id",dish.getId());
            List f = dishFlavorsService.list(wrapper1);
            dish.setFlavors(f);
        }

        return list;
    }
}
